
PYTHON=$(shell which python)



SYSTEM=$(shell $(PYTHON) -c "import platform; print platform.system()")
ifeq (Linux, $(SYSTEM))
LIBEVENTDLL=libevent.so
endif
ifeq (Darwin, $(SYSTEM))
LIBEVENTDLL=libevent.dylib
ECHOOPTS=-e
endif
ifndef LIBEVENTDLL
	fail
endif



egg: eggs
eggs: clean libeventdll
	$(PYTHON) setup.py bdist_egg

all:	eggs

distclean: fullclean
mrproper: fullclean
fullclean: clean
	@echo "[*] removing _all_ automatically generated files"
	rm -f evserver/libevent.so evserver/libevent.dylib
	rm -f evserver/ctypes_event_*.py
clean:
	find . -name "*.pyc" | xargs rm -f
	find . -name "*.pyo" | xargs rm -f
	rm -rf build dist evserver.egg-info
	rm -f /tmp/fifo
	rm -f log

propset: clean
	find evserver Makefile *py README -type f -a -not -path \*.svn\* |xargs svn propset svn:mime-type 'text/plain'
	svn propset svn:mime-type 'application/octet-stream' evserver/$(LIBEVENTDLL)

# needs:
#   sudo apt-get install gccxml python-setuptools
#   sudo easy_install ctypeslib
#   libevent >= 1.4.2 compiled sources in LIBEVENTSOURCES
#   (1.4.1 has a bug in chunked encoding and requires patching)
#   libevent-1.4.6, libevent-1.4.7, libevent-1.4.8 *DON'T* work for me.
#      no time still to investigate where is the bug
#   for example:
#       cd /tmp/libevent
#	./configure --prefix=/usr
#	make

# use libevent sources, this will need the application to be runned with LD_LIBRARY_PATH
ifndef LIBEVENTSOURCES
ifeq (ok, $(shell [ -e "../libevent" ] && echo ok))
LIBEVENTSOURCES=../libevent
endif
ifeq (ok, $(shell [ -e "/tmp/libevent" ] && echo ok))
LIBEVENTSOURCES=/tmp/libevent
endif
endif

LIBEVENTLIB=$(LIBEVENTSOURCES)/.libs
LIBEVENTINCLUDE=$(LIBEVENTSOURCES)
LIBEVENTPRELOAD=$(LIBEVENTLIB)/$(LIBEVENTDLL)

VERSION=$(shell $(PYTHON) -c "import evserver.main; print evserver.main.get_libevent_version('$(LIBEVENTPRELOAD)').replace('-','_').replace('.','_')"  )
CTYPESPY=evserver/ctypes_event_$(VERSION).py

# figuring this out was painful
#$ :D_LIBRARY_PATH="." python -c "import ctypes; ctypes.CDLL('libevent.so')";
bindings: libeventdll
	@if [ ! -e "$(LIBEVENTPRELOAD)" ] ; then \
	echo "Not found $(LIBEVENTPRELOAD). Check the libevent sources path in LIBEVENTSOURCES." ;\
	echo "You should run: LIBEVENTSOURCES=../my/path/to/compiled/libevent make"; \
	exit 1 ;\
	fi

	@if [ ! -e "$(LIBEVENTINCLUDE)/http-internal.h" ] ; then \
	echo "Not found $(LIBEVENTINCLUDE)/http-internal.h. Check the libevent sources path in LIBEVENTSOURCES." ;\
	echo "You should run: LIBEVENTSOURCES=../my/path/to/compiled/libevent make"; \
	exit 1 ;\
	fi

	# testing dependencies
	@gccxml --version
	@python -c "import ctypeslib; import sys; sys.exit(0)"

	@echo "[*] preparing headers"
	cp $(LIBEVENTINCLUDE)/event.h .
	cp $(LIBEVENTINCLUDE)/http-internal.h .

	@echo $(ECHOOPTS)  "\
	#ifndef TAILQ_ENTRY	\n\
	#define _EVENT_DEFINED_TQENTRY	\n\
	#define TAILQ_ENTRY(type)                                               \\\\\n\
	struct {                                                                \\\\\n\
		struct type *tqe_next;  /* next element */                      \\\\\n\
		struct type **tqe_prev; /* address of previous next element */  \\\\\n\
	}	\n\
	#endif /* !TAILQ_ENTRY */	\n\
	" > evhttp.h
	cat $(LIBEVENTINCLUDE)/evhttp.h >> evhttp.h

	@echo $(ECHOOPTS)  "\
	extern \"C\" {	\n\
	#import <stdlib.h>	\n\
	#import <sys/queue.h>	\n\
	#import <sys/socket.h>	\n\
	#import \"event.h\"	\n\
	#import \"evhttp.h\"	\n\
	#import \"http-internal.h\"	\n\
	}	\n\
	" > d.h

	@echo $(ECHOOPTS) "\
	# don't touch. automatically generated by 'make ctypes' \n\
	# for file $(LIBEVENTPRELOAD) \n\
	import ctypes	\n\
	c_longdouble = ctypes.c_double \n\
	" > $(CTYPESPY)

	@echo "[*] magick happens" --preload "$(LIBEVENTPRELOAD)"
	h2xml.py -I"$(LIBEVENTINCLUDE)" $(PWD)/d.h -o d.xml -c -q
	sed  "s/13u/13/g" d.xml > d2.xml
	sed  "s/15u/13/g" d2.xml > d.xml
	LD_LIBRARY_PATH=$(LIBEVENTLIB) xml2py.py -l$(LIBEVENTDLL) -d d.xml  -r "(event_|signal_|evbuffer_|evhttp|evtimer|EV).*" >> $(CTYPESPY)
	# fix output
	sed  "s/CDLL('$(LIBEVENTDLL)')/CDLL(libeventbinary)/g" $(CTYPESPY) > $(CTYPESPY)2
	mv $(CTYPESPY)2 $(CTYPESPY)

	@echo "[*] new bindings to libevent are in $(CTYPESPY)"
	rm d.xml d2.xml evhttp.h event.h http-internal.h d.h

libeventdll:
	@if [ ! -e "$(LIBEVENTPRELOAD)" ] ; then \
	echo "Not found $(LIBEVENTPRELOAD). Check the libevent sources path in LIBEVENTSOURCES." ;\
	echo "You should run: LIBEVENTSOURCES=../my/path/to/compiled/libevent make"; \
	exit 1 ;\
	fi
	rm -f evserver/libevent.so evserver/libevent.dylib
	cp $(LIBEVENTPRELOAD) evserver/$(LIBEVENTDLL)

test:
	PYTHONPATH="." $(PYTHON) test.py
	rm -f log

prepare: clean ctypes



